<?php

class Home extends Controller {

	function Home(){
		parent::Controller();	
	}
	function index(){
		if($this->session->userdata('isLogin')){
				redirect('/dashboard/');
		}else{
			$this->load->model('User');
			$this->load->model('UserMahasiswa');
			$this->load->model('UsersDetail');
			$data['newMember'] = $this->User->getAllMembers(5);
			for ($i=0;$i<sizeof($data['newMember']['id']);$i++){
				$data['user_id'][$i] =  $data['newMember']['id'][$i];
				$data['username'][$i] = $this->User->getUsername( $data['newMember']['id'][$i]);
				$data['nama'][$i] = json_decode($this->UserMahasiswa->getNama( $data['newMember']['id'][$i]))->nama;
				$detail = json_decode($this->UsersDetail->getDetail( $data['newMember']['id'][$i]));
				$data['avatar'][$i] = $detail->avatar;
				if($data['avatar'][$i]==''){
					$data['avatar'] [$i]= 'default_avatar.jpg';
				}
			}
			$data ['pageTitle'] = 'Selamat Datang di I-See, Jejaring Sosial Ilkomers';
			$this->load->view('public_home',$data);
		}
	}
	function login(){
		
		$data ['pageTitle'] = 'Selamat Datang di I-See, Silakan Lakukan Login';
		$data['error']=FALSE;
		$data['errorMsg']='';
		$data['username'] = '-Username/NIM-';
		$data['password'] = '-Your Password-';
		if($this->input->post('username')){
			$data['username'] = $this->input->post('username');
			$data['password'] = $this->input->post('password');
			$this->load->model('UserMahasiswa');
			$login = $this->UserMahasiswa->login($data['username'],$data['password']);
			$data['errorMsg'] = $login['error'];
			if($data['errorMsg']!=''){
				$data['error']=TRUE;
			}else{
				$this->load->model('User');
				$this->User->setLogDate($login['id']);
				$userData = array(
                   'userId'  => $login['id'],
                   'isLogin' => TRUE
               	);
				$this->session->set_userdata($userData);
				redirect('/dashboard/');
			}
		}
		$this->load->view('login',$data);
	}
	
	function signup(){
	
		$data ['pageTitle'] = 'I-See | Registrasi';
		$data['error']=FALSE;
		$data['errorMsg']='';
		$data['nim'] = '-Enter your NIM-';
		if($this->input->post("nim")){
			$data['nim'] = $this->input->post("nim");
			$this->load->model('Mahasiswa');
			$sendMail = $this->_sendMail($data['nim']);
			if($sendMail->success){
				$data['error']=TRUE;
				$data['errorMsg']='Email konfirmasi telah dikirm ke '.$sendMail->email.'';
			}else{
				$data['error']=TRUE;
				$data['errorMsg']=$sendMail->error;
			}
		}
		$this->load->view('signup',$data);
	}
	
	function _sendMail($nim){
		$result->success = TRUE;
		$result->error ='';
		$this->load->model('Mahasiswa');
		$this->load->model('User');
		$return = json_decode($this->Mahasiswa->isIlkomers($nim));
		if($return->ok){
			srand ((double) microtime( )*1000000);
			$random_number = rand( );
			$linkConfirm = md5($random_number.'isee');
			$mid = $return->id;
			$user = json_decode($this->User->insertnewuser($mid,$linkConfirm));
			if(!$user->ketersediaan){
				$result->success = FALSE;
				$result->error = 'Anda sudah melakukan registrasi sebelumnya, silakan cek email anda';
			}else{
				$message = "Selamat Datang di I-See, Situs Jejaring Sosial\n
				Silakan Klik Link Konfirmasi di bawah ini \n
				".base_url()."registrasi/index/".$user->id."/".$linkConfirm."\n
				\n
				I-See Team";
				$subject = "I-See Link Confirmation";
				$email = $return->email;
				$from	= 'I-See Team'.' <'.'ilkomersee@gmail.com'.'>';	
				if (email_send($from , $email, $subject, $message)){
					$result->success = TRUE;
					$result->email = $email;
				}else{
					$result->success = FAL